/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.editor.sheet.uml;

import java.awt.BorderLayout;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import self.amigo.editor.APropertySheet;
import self.amigo.editor.cmd.PropertiesChange;
import self.amigo.elem.uml.ArchetypeUtils;
import self.amigo.elem.uml.AuxilaryCompartmentFeature;
import self.amigo.elem.uml.LogicalClassView;
import self.amigo.elem.uml.NameCompartmentFeature;
import self.awt.layout.VerticalFlowLayout;
import self.gee.IEditableElement;
import self.gee.editor.IPropertySheet;
import self.lang.BooleanUtils;

public class LogicalClassSheet extends APropertySheet implements IPropertySheet {
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  JTabbedPane classViews = new JTabbedPane();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel generalPanel = new JPanel();
  JLabel nameLabel = new JLabel();
  VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
  JTextField name = new JTextField();
  JLabel stereotypeLabel = new JLabel();
  JTextField stereotype = new JTextField();
  JLabel namespaceLabel = new JLabel();
  JTextField namespace = new JTextField();
  JCheckBox isAbstract = new JCheckBox();
  JPanel attrPanel = new JPanel();
  VerticalFlowLayout verticalFlowLayout2 = new VerticalFlowLayout();
  JCheckBox attrIsVisible = new JCheckBox();
  JScrollPane attrScroller = new JScrollPane();
  JTextArea attributes = new JTextArea();
  JPanel operationsPanel = new JPanel();
  JCheckBox opIsVisible = new JCheckBox();
  VerticalFlowLayout verticalFlowLayout3 = new VerticalFlowLayout();
  JScrollPane opScroller = new JScrollPane();
  JTextArea operations = new JTextArea();
  JLabel aidLinkLabel = new JLabel();
  JTextField aidLink = new JTextField();
  BorderLayout borderLayout2 = new BorderLayout();
  JLabel archetypeLabel = new JLabel();
  JComboBox archetypeColor = new JComboBox();

  BorderLayout generalRegionHostLayout = new BorderLayout();
  JScrollPane generalRegionScroller = new JScrollPane();
  JPanel      generalTab = new JPanel();
  BorderLayout attrRegionHostLayout = new BorderLayout();
  JScrollPane attrRegionScroller = new JScrollPane();
  JPanel      attrTab = new JPanel();
  BorderLayout oprRegionHostLayout = new BorderLayout();
  JScrollPane oprRegionScroller = new JScrollPane();
  JPanel      oprTab = new JPanel();

  private IEditableElement view;
  private NameCompartmentFeature     general;
  private AuxilaryCompartmentFeature attr;
  private AuxilaryCompartmentFeature oper;

  public LogicalClassSheet() {
    try {
      jbInit();
      archetypeColor.setModel( ArchetypeUtils.createArchetypeModel() );
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    nameLabel.setDisplayedMnemonic('N');
    nameLabel.setLabelFor(name);
    nameLabel.setText("name");
    name.setFocusAccelerator('N');
    generalPanel.setLayout(verticalFlowLayout1);
    stereotypeLabel.setText("stereotype");
    archetypeLabel.setText("archetype color");
    namespaceLabel.setText("namespace");
    isAbstract.setText("abstract");
//    isAbstract.setHorizontalTextPosition(SwingConstants.LEADING);
    attrPanel.setLayout(verticalFlowLayout2);
    attrIsVisible.setText("visible");
//    attrIsVisible.setHorizontalTextPosition(SwingConstants.LEADING);
    attributes.setRows(10);
    attributes.setWrapStyleWord(true);
    opIsVisible.setText("visible");
//    opIsVisible.setHorizontalTextPosition(SwingConstants.LEADING);
    operationsPanel.setLayout(verticalFlowLayout3);
    operations.setRows(10);
    operations.setWrapStyleWord(true);
    aidLinkLabel.setDisplayedMnemonic('0');
    aidLinkLabel.setText("aid");
    generalPanel.add(nameLabel, null);
    generalPanel.add(name, null);
    generalPanel.add(stereotypeLabel, null);
    generalPanel.add(stereotype, null);
    generalPanel.add(archetypeLabel, null);
    generalPanel.add(archetypeColor, null);
    generalPanel.add(namespaceLabel, null);
    generalPanel.add(namespace, null);
    generalPanel.add(isAbstract, null);
    generalPanel.add(aidLinkLabel, null);
    generalPanel.add(aidLink, null);
    attrPanel.add(attrIsVisible, null);
    attrPanel.add(attrScroller, null);
    operationsPanel.add(opIsVisible, null);
    operationsPanel.add(opScroller, null);
    opScroller.getViewport().add(operations, null);
    attrScroller.getViewport().add(attributes, null);

    generalTab.setLayout( generalRegionHostLayout );
    generalTab.add( generalRegionScroller, BorderLayout.CENTER );
    generalRegionScroller.getViewport().add( generalPanel, null );

    oprTab.setLayout( oprRegionHostLayout );
    oprTab.add( oprRegionScroller, BorderLayout.CENTER );
    oprRegionScroller.getViewport().add( operationsPanel, null );

    attrTab.setLayout( attrRegionHostLayout );
    attrTab.add( attrRegionScroller, BorderLayout.CENTER );
    attrRegionScroller.getViewport().add( attrPanel, null );

    this.add(classViews, BorderLayout.CENTER);
    classViews.add(generalTab, "General");
    classViews.add(oprTab, "Operations");
    classViews.add(attrTab, "Attributes");
  }

  public void inspect( Object toDisplay ) {
    if ( toDisplay instanceof LogicalClassView ) {
      view = (IEditableElement) toDisplay;
      revert();
    } else
      throw new RuntimeException( "Invalid Graphic element to display:" + toDisplay );
  }

  public void accept() {
    if ( view == null )
      return;
    store.put( LogicalClassView.ABSTRACT_PROP, BooleanUtils.toBoolean(isAbstract.isSelected()) );

    general.name = name.getText();

    general.stereotype = stereotype.getText();
    general.archetypeColor = archetypeColor.getSelectedIndex();
    general.nameSpace = namespace.getText();
    general.aidLink = aidLink.getText();

    attr.visible = attrIsVisible.isSelected();
    attr.details = attributes.getText();
    oper.visible = opIsVisible.isSelected();
    oper.details = operations.getText();

    PropertiesChange cmd = new PropertiesChange( editor, view, store );
    cmd.executeIfRequired();
  }

  public void revert() {
    if ( view == null )
      return;
    store.clear();
    view.getProperties( store );

    general = (NameCompartmentFeature) store.get( LogicalClassView.NAMECOMPARTMENT_PROP );
    attr = (AuxilaryCompartmentFeature) store.get( LogicalClassView.ATTRIBUTESCOMPARTMENT_PROP );
    oper = (AuxilaryCompartmentFeature) store.get( LogicalClassView.OPERATIONSCOMPARTMENT_PROP );

    name.setText( general.name );
    stereotype.setText( general.stereotype );
    archetypeColor.setSelectedIndex( general.archetypeColor );
    namespace.setText( general.nameSpace );
    aidLink.setText( general.aidLink );

    isAbstract.setSelected( ((Boolean)store.get(LogicalClassView.ABSTRACT_PROP)).booleanValue() );
    attrIsVisible.setSelected( attr.visible );
    opIsVisible.setSelected( oper.visible );
    attributes.setText( attr.details );
    operations.setText( oper.details );
  }

  public Object getObjectUnderInspection() {
    return view;
  }

//  static public void main( String[] args ) {
//    JFrame form = new JFrame( "test" );
//    ClassSheet sheet = new ClassSheet();
//    form.getContentPane().add( sheet );
//    form.show();
//  }
}
